package com.jzh.selfwallpaper.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;

import com.jzh.selfwallpaper.R;
import com.jzh.selfwallpaper.constant.Constant;

public class ColorChooseAdapter extends BaseAdapter {

    private int[] colors;
    private Context context;
    private LayoutInflater inflate;
    private int checked_position = 0;
    private OnGridViewClicked onGridViewClicked;

    public void setOnGridViewClicked(OnGridViewClicked onGridViewClicked) {
        this.onGridViewClicked = onGridViewClicked;
    }

    public interface OnGridViewClicked {
        void onGridItemClicked(int position);
    }

    public ColorChooseAdapter(int[] colors, Context context) {
        this.colors = colors;
        this.context = context;
        if (context != null) {
            inflate = LayoutInflater.from(context);
        }
    }

    @Override
    public int getCount() {
        return colors.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        Holder holder;
        if (view == null) {
            view = inflate.inflate(R.layout.item_choose_color_gv, null);
            holder = new Holder(view);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        holder.view_color_item.setBackgroundColor(context.getResources().getColor(colors[position]));
        int color = checked_position == position ? R.color.A13 : R.color.A4;
        holder.rl_color_item_choosed.setBackgroundColor(context.getResources().getColor(color));
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checked_position = position;
                if (onGridViewClicked != null) {
                    onGridViewClicked.onGridItemClicked(position);
                }
            }
        });
        return view;
    }

    private static class Holder {

        View view_color_item;
        RelativeLayout rl_color_item_choosed;

        public Holder(View view) {
            this.view_color_item = view.findViewById(R.id.view_color_item);
            rl_color_item_choosed = view.findViewById(R.id.rl_color_item_choosed);
        }
    }
}
